<?php

namespace app\shop\controller\order;

use app\shop\controller\Controller;
use app\shop\model\order\Order as OrderModel;
use app\shop\model\supplier\Supplier as SupplierModel;

/**
 * 订单操作
 * @package app\shop\controller\order
 */
class Operate extends Controller
{
    /**
     * 订单导出
     */
    public function export($dataType)
    {
        $model = new OrderModel();

        $map = [];
        $params = $this->postData();
        if (isset($params['province_id']) && $params['province_id']) {
            $map[] = ['province_id', '=', $params['province_id']];
        }
        if (isset($params['city_id']) && $params['city_id']) {
            $map[] = ['city_id', '=', $params['city_id']];
        }
        if (isset($params['region_id']) && $params['region_id']) {
            $map[] = ['region_id', '=', $params['region_id']];
        }

        if (isset($params['shop_supplier_name']) && $params['shop_supplier_name']) {
            $map[] = ['name', 'like', '%' . trim($params['shop_supplier_name']) . '%'];
        }

        $shop_supplier_ids = [];
        if($map){
            $shop_supplier_ids = (new SupplierModel()) -> where($map) -> column('shop_supplier_id');
            $params['shop_supplier_ids'] = $shop_supplier_ids;
        }

        return $model->exportList($dataType, $params);
    }

    /**
     * 审核：用户取消订单
     */
    public function confirmCancel($order_id)
    {
        $model = OrderModel::detail($order_id);
        if ($model->confirmCancel($this->postData())) {
            return $this->renderSuccess('操作成功');
        }
        return $this->renderError('操作失败');
    }

    /**
     * 门店自提核销
     */
    public function extract()
    {
        $params = $this->postData('extract_form');
        $model = OrderModel::detail($params['order_id']);
        if ($model->verificationOrder($params['order']['extract_clerk_id'])) {
            return $this->renderSuccess('核销成功');
        }
        return $this->renderError($model->getError() ?: '核销失败');
    }

}